package com.sljy.hzk.util;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.sljy.hzk.MainActivity;
import com.sljy.hzk.R;
import com.sljy.hzk.ui.exam.ExamMainActivity;

public class NotificationService extends Service {

    // 获取消息线程
    private MessageThread messageThread = null;

    // 点击查看
    private Intent messageIntent = null;
    private PendingIntent messagePendingIntent = null;

    // 通知栏消息
    private int messageNotificationID = 1000;
    private Notification messageNotification = null;
    private NotificationManager messageNotificatioManager = null;

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 初始化
        //ShowNotification();
        // 开启线程
        messageThread = new MessageThread();
        messageThread.isRunning = true;
        messageThread.start();
        return START_REDELIVER_INTENT;
       // return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 从服务器端获取消息
     *
     */
    class MessageThread extends Thread {
        // 设置是否循环推送
        public boolean isRunning = true;
        @Override
        public void run() {
            while (isRunning) {
                try {
                    // 间隔时间
                    Thread.sleep(5000);
                    // 获取服务器消息
                    String serverMessage = getServerMessage();
                    if (serverMessage != null && !"".equals(serverMessage)) {
                        // 更新通知栏
                        //SendMessage("SSSS");
                        ShowNotification();
                        // 每次通知完，通知ID递增一下，避免消息覆盖掉
                        messageNotificationID++;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    private void ShowNotification(){
        Notification.Builder mBuilder = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("20180807")
                .setContentText("测试测试");
        Intent intent = new Intent(this, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(intent);
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = mBuilder.build();
        nm.notify(0,notification);
        startForeground(1,notification);
    }

    public void SendMessage(String str){
        int icon = R.mipmap.ic_launcher;
        long when = System.currentTimeMillis();
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(when)
                .setSmallIcon(icon)
                .setTicker("Hearty365")
                .setContentTitle("通知标题")
                .setContentText("我是消息")
                .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
                .setContentIntent(contentIntent)
                .setContentInfo("Info");

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());
    }

    @Override
    public void onDestroy() {
        // System.exit(0);
        Log.i("销毁进程","销毁了");
        messageThread.isRunning = false;
        stopForeground(true);
        super.onDestroy();
        messageThread = new MessageThread();
        messageThread.isRunning = true;
        messageThread.start();
    }

    /**
     * 模拟发送消息
     *
     * @return 返回服务器要推送的消息，否则如果为空的话，不推送
     */
    public String getServerMessage() {
        return "NEWS!";
    }
}
